<?php 
$course=""; 
$Year="";
$courseerr="";
$yearerr="";
$comment="";
session_start();
include "dbconnect.php";
$hireUsername="";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=1)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}

		$post=0;
		$result="";
		$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
		mysql_select_db($_DB_NAME,$con);
		$result1=mysql_query("select distinct year from semester order by year desc");//Need to depend on the selected semester

			if(isset($_SESSION["Semterm"]) and isset($_REQUEST["fromHire"]))//Used for redirect from Hire
				{
			
			$course=$_SESSION["Course"];
			$semType=$_SESSION["Semterm"];
			$Year=$_SESSION["Year"];
			
			mysql_query("SET @pterm='".$_SESSION["Semterm"]."'");
			mysql_query("SET @pyear='".$_SESSION["Year"]."'");
			mysql_query("set @courseid= '".$_SESSION["Course"]."'");
			$result=mysql_query("call getstudentinfo(@courseid,@pyear,@pterm)");
			
			$post=1;
			mysql_close($con);
			$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
			mysql_select_db($_DB_NAME,$con);
			
						$usernames=mysql_query("select username from hire, openings where hire.openingid=openings.openingid and openings.courseid='".$_SESSION["Course"]."'");
						
						mysql_close($con);
			$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
			mysql_select_db($_DB_NAME,$con);
			
			mysql_query("SET @pterm='".$_SESSION["Semterm"]."'");
			mysql_query("SET @pyear='".$_SESSION["Year"]."'");
			mysql_query("set @courseid= '".$_SESSION["Course"]."'");
			$commentrs=mysql_query("select comment from openings,semester where courseid=@courseid and term=@pterm and year=@pyear and openings.semid=semester.semid");
			if($commentrs==false)
			{
				die(mysql_error());
			}
			 if($commentrow=mysql_fetch_array($commentrs))
			 {
				 $comment=$commentrow["comment"];
				 
				 }
						
						$usernameset=array();
						$i=0;
						while($row=mysql_fetch_array($usernames))
						{
							$usernameset[$i]=$row[0];
							$i++;
						}
						
			
			
				}
				else
				{
				unset($_SESSION["Course"]);
				unset($_SESSION["Semterm"]);
				unset($_SESSION["Year"]);
				}
		
		
		
	if ($_SERVER["REQUEST_METHOD"] == "POST")
	{
			mysql_close($con);
			$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
			mysql_select_db($_DB_NAME,$con);
				$course=$_POST["courseid"];
				$semType=$_POST["hireSem"];
				$Year=$_POST["year"];
				if (empty($course))
					{
					
						$courseerr = "Course is required";
						
					}
					
					
						
					
				if(empty($Year))
					{
						$yearerr="Enter a year";
					}
					else if(!preg_match("/^[0-9]{4}$/",$Year))
			   		{
					  $yearerr = "Invalid Year format";
			   		}
					if($courseerr=="" and $yearerr=="")
					{
				
			
			
			mysql_query("SET @pterm='".$_POST["hireSem"]."'");
			mysql_query("SET @pyear='".$_POST["year"]."'");
			mysql_query("set @courseid= '".$_POST["courseid"]."'");
			$result = mysql_query("call getstudentinfo(@courseid,@pyear,@pterm)");
			if($result==false)
			{
				die(mysql_error());
				}
			$post=1;
			
			//mysql_query("set @popenid= ".$openingid["openingid"]);
			mysql_close($con);
			$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
			mysql_select_db($_DB_NAME,$con);
			
						$usernames=mysql_query("select username from hire, openings where hire.openingid=openings.openingid and openings.courseid='".$_POST["courseid"]."'");
						$usernameset=array();
						$i=0;
						while($row=mysql_fetch_array($usernames))
						{
							$usernameset[$i]=$row[0];
							$i++;
						}
							
					
			mysql_close($con);
			$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
			mysql_select_db($_DB_NAME,$con);
			
			mysql_query("SET @pterm='".$_POST["hireSem"]."'");
			mysql_query("SET @pyear='".$_POST["year"]."'");
			mysql_query("set @courseid= '".$_POST["courseid"]."'");
			$commentrs=mysql_query("select comment from openings,semester where courseid=@courseid and term=@pterm and year=@pyear and openings.semid=semester.semid");
			if($commentrs==false)
			{
				die(mysql_error());
			}
			 if($commentrow=mysql_fetch_array($commentrs))
			 {
				 $comment=$commentrow["comment"];
				 
				 }
			//Setting all session variables;
			$_SESSION["Course"]=$_POST["courseid"];
			$_SESSION["Semterm"]=$_POST["hireSem"];
			$_SESSION["Year"]=$_POST["year"];
			
			
			
			if($result == false)
			{
				die(mysql_error());
				}			
			
					}
		}
?>
	

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Hire</title>

<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>

<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />
<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-ui-1.10.3/jquery-ui-1.10.3/ui/jquery-ui.js"></script>

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
            // Create jqxExpander
           $('#datatables').dataTable(
	
	{	"bJQueryUI":true,"oLanguage": {
      "sEmptyTable": "No students have applied to this opening"}
		});
			$("#courseid").keyup(function(){ 
    				$.ajax({
						type: "POST",
                		url: "autoSuggest.php",
                		data: {option: 1, courseid: $("#courseid").val()},
					    success: function(courses){
									$("#courseid").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");	}
					});
			});
        });
</script>

 <script type="text/javascript">
		function populateYear()
		{
			$.ajax({
						type: "POST",
                		url: "PopulateYear.php",
                		data: {term :document.getElementById("term").value,year: document.getElementById("year").value},
					   success: function(courses){
									$("#year").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
		</script>  
<script>
function set(value, text)
{		
		var atext=text;
		 atext=atext.trim();
		var split=value.split(',');
		username=split[0];
		openingid=split[1]; 
		if(atext=="assign")
	  		window.location.href = 'hire.php?hireUsername='+ username+'&courseid='+openingid ;
		else
			window.location.href = 'unhire.php?hiredusername='+username+'&openingid='+openingid;

 }
</script>	


<style>
</style>
</head>

<body style="min-width:1280px">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img  height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey">
</div>
<center>
<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->

              <ul>
              	
               <li ><a href="hireView.php">Hire</a></li>
                <li ><a href="post.php">Openings</a></li>
                <li ><a href="budget.php">Budget</a></li>
                <li ><a href="semester.php">Semester</a></li>
                <li ><a href="manage.php">Manage</a></li>
              </ul>          

</div>
</center>
	<span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
         <div class="footbody">
         <form method="post"  action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"])); ?>" autocomplete="off" enctype="multipart/form-data">
             <table>
             <thead>
             <tr >
             <td rowspan="2" colspan="2">
             <label class="Header1">Hiring Process</label>
             </td>
             </tr>
             </thead>

             <tbody>
             <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
             <tr>
                  <td>
                  <label style="color:#990000" class="left-alignmentlev1">Select a semester </label>
                  </td>
                  <td />
                  <td>
                <!--  <select  name="hireSem" class="crimson-text">
                    <option value="FALL">Fall</option>
                    <option value="SPRING">Spring</option>
                  </select>-->
                  <select name="hireSem" id="term" class="crimson-text"  >
                    
                     <option value="FALL"<?php
					  if (isset($semType) && $semType=="FALL") echo "selected='selected'";
					  else echo "";?>>Fall</option>
                   <option value="SPRING"<?php
					  if (isset($semType) && $semType=="SPRING") echo "selected='selected'";
					  else echo "";?>>Spring</option>

                  </select>
                  </td>
               </tr>
               <tr />
               <tr />
               <tr />
               <tr />
               <tr />
               <tr />
               <tr/>
                <tr/>
                <tr/>
                <tr/>
               <tr>
                   <td>
                  <label style="color:#990000" class="left-alignmentlev1"  >Provide a year </label>
                  </td>
                  <td />
                  <td>
                   <input type="text" id="year" name="year" class="crimson-border" onkeyup="populateYear()" value="<?php echo $Year;?>"/>   
                 </td>
                  <td class="error">
    <?php echo $yearerr;?>
               </tr>
                <tr />
               <tr />
               <tr />
               <tr />
               <tr />
               <tr />
               <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr>
                   <td>
                  <label style="color:#990000" class="left-alignmentlev1">Select a course </label>
                  </td>
                  <td />
                  <td>
                   <input id="courseid" name="courseid"type="text" class="crimson-border"  value="<?php echo $course;?>"/>
                  </td>
                   <td class="error">
     <?php echo $courseerr;?>
                  </td>
                </tr>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                <tr/>
                    <tr>
                    <td rowspan="2">
                  <input type="submit" value="Submit" ></input>
                  </td>
                  </tr>
              </tbody>
           </table>
           </form>
           <br /><br />
           <label class="crimson-text" style="vertical-align:middle">Comments</label><br/>
           <Textarea style="width:300px;" disabled class="crimson-border"><?php echo $comment;?></Textarea>
              <div style="margin-top:30px;">
              <table id="datatables" class="display" >
              <thead>
              <tr>
              <th>Name</th>
              <th>Degree</th>
              <th>Experience</th>
              <th>Grade</th>
              <th>TEPAIC</th>
              <th>Assign</th>
              </tr>
              </thead>
              <tbody>
              <?php 
			  if($post==1)
			  {
              while($row=mysql_fetch_array($result)){
                  ?>
                  <tr>
                  <td><?= $name= $row['Firstname']." ".$row['Lastname'];?></td>
                  <td><?=$row['description']?></td>
                  <td><?=$row['Experience']?></td>
                  <td><?=$row['grade']?></td>
                  <td><?=$row['TEPAIC']?></td>
                   <td>
                    <a  data-value=<?php echo $row['username'].",".$row["openingid"]?> onclick="set($(this).data('value'),$(this).html())">
                    <?php
						
						if(in_array($row['username'],$usernameset)) echo "Unassign";
						else
					     echo "assign"; ?>
                    </a>
                    </td>
                  <?php }}?>
                  </tr>
              </tbody>
              </table>
              </div>
           </div>
    </body>
</html>